﻿function rearrange_fields_by_type(form_fields_data, account_type) {
  var at_ctrl = jQuery('#Profile_account_type');
  if(account_type == 1) {
    at_ctrl.val(1);
  } else {
    at_ctrl.val(0);
  }
  
  jQuery.each(form_fields_data.company_fields, function(name, value) {
    if(account_type == 1) {
      show_elem(name, value, form_fields_data.fake_field_ctrl_val);
    } else {
      hide_elem(name, value, form_fields_data.fake_field_ctrl_val);
    }
  });
  
  jQuery.each(form_fields_data.only_private_fields, function(name, value) {
    
	if(account_type == 1) {
	  hide_elem(name, value, form_fields_data.fake_field_ctrl_val);
    } else {
	  show_elem(name, value, form_fields_data.fake_field_ctrl_val);
    }
  });
  
  jQuery.each(form_fields_data.mod, function(name, value) {
    var _label =  jQuery('label[for=Profile_' + name + ']');
    var _children = _label.children();
    var desc = (account_type == 1 ? value.company_desc : value.private_desc);
    
    _label.text(desc + ' ');
    if(_children) {
      _label.append(_children);
    }
  });
}

function hide_elem(elem_selector, is_required, ctrl_val) {
  jQuery('#' + elem_selector).addClass('hidden');
  var elem = jQuery('#' + 'Profile_' + elem_selector);
  if(is_required === "1") {
	if(elem.val().length == 0) {
	  elem.val(ctrl_val);
	}
  }
}

function show_elem(elem_selector, is_required, ctrl_val) {
  jQuery('#' + elem_selector).removeClass('hidden');
  var elem = jQuery('#' + 'Profile_' + elem_selector);
  if(is_required === "1") {
    if(elem.val() == ctrl_val) {
	  elem.val('');
	}
  }
}
